clear
version 15.1

* set to project directory

capture cd "project directory" 

*Install mat2txt
*ssc install mat2txt

global r = 1000  // set number of repetitions for bootstrap - 1000 for replication


use Data/dd, clear


sort no_election
tsset no_election


************************************** 
***** Table 1 ******
***1792_2016***
*run again

bootstrap, reps($r) seed(418):   reg  runagain gdpchange              if termed==.
         outreg2 using Output\Table_1a_1792_2016, se dec(3) replace noas label
bootstrap, reps($r) seed(418):   reg runagain gdpchange death         if termed==.
     estat dwatson
        outreg2 using Output\Table_1a_1792_2016, se dec(3) append noas  label
bootstrap, reps($r) seed(418):   reg  runagain gdpchange death war    if termed==.
     estat dwatson
        outreg2 using Output\Table_1a_1792_2016, se dec(3) append noas label
bootstrap, reps($r) seed(418):   reg  runagain gdpchange death war  inc_yrs  domparty  if termed==.
         outreg2 using Output\Table_1a_1792_2016, se dec(3) append noas label
*win
bootstrap, reps($r) seed(418):   reg  win gdpchange               if termed==.
         outreg2 using Output\Table_1a_1792_2016, se dec(3) append noas  label
bootstrap, reps($r) seed(418):   reg  win gdpchange death         if termed==.
         outreg2 using Output\Table_1a_1792_2016, se dec(3) append noas  label
bootstrap, reps($r) seed(418):   reg  win gdpchange death war     if termed==.
         outreg2 using Output\Table_1a_1792_2016, se dec(3) append noas  label
bootstrap, reps($r) seed(418):   reg  win gdpchange death war  inc_yrs domparty    if termed==.
         outreg2 using Output\Table_1a_1792_2016, se dec(3) append noas  label
bootstrap, reps($r) seed(418):   reg  win gdpchange death war  inc_yrs domparty multi   if termed==.
         outreg2 using Output\Table_1a_1792_2016, se dec(3) append noas  label word
***1792_2044***
*run again
bootstrap, reps($r) seed(418):    reg  runagain gdpchange           if termed==. & year <= 1944
        outreg2 using Output\Table_1b_1792_1944, se dec(3) replace noas  label
bootstrap, reps($r) seed(418):    reg runagain gdpchange death      if termed==.  & year <= 1944
        outreg2 using Output\Table_1b_1792_1944, se dec(3) append noas  label
bootstrap, reps($r) seed(418):    reg  runagain gdpchange death war if termed==. & year <= 1944
        outreg2 using Output\Table_1b_1792_1944, se dec(3) append noas label
bootstrap, reps($r) seed(418):    reg  runagain gdpchange death war inc_yrs domparty if termed==. & year <= 1944
         outreg2 using Output\Table_1b_1792_1944, se dec(3) append noas label
*win
bootstrap, reps($r) seed(418):    reg  win gdpchange           if termed==. & year <= 1944
         outreg2 using Output\Table_1b_1792_1944, se dec(3) append noas  label
bootstrap, reps($r) seed(418):    reg  win gdpchange death     if termed==. & year <= 1944
         outreg2 using Output\Table_1b_1792_1944, se dec(3) append noas  label
bootstrap, reps($r) seed(418):    reg  win gdpchange death war if termed==. & year <= 1944
         outreg2 using Output\Table_1b_1792_1944, se dec(3) append noas  label
bootstrap, reps($r) seed(418):    reg  win gdpchange death war  inc_yrs domparty if termed==. & year <= 1944
         outreg2 using Output\Table_1b_1792_1944, se dec(3) append noas  label
bootstrap, reps($r) seed(418):    reg  win gdpchange death war  inc_yrs domparty multi if termed==. & year <= 1944
         outreg2 using Output\Table_1b_1792_1944, se dec(3) append noas  label word

	

************************************** 
***** Table 2 ******
*********TABLE TWO: ROBUSTNESS******
* Robustness table 
matrix a = J(40,7,.) // Create matrix
matrix colnames a = b SE R2 b2 SE2 R22 N2
local i = 1
bootstrap, reps($r) seed(418):  reg  runagain gdpchange  if termed==.
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
 bootstrap, reps($r) seed(418): reg  win gdpchange  if termed==.
   matrix a[`i',4] = round(_b[gdpchange],.001)
   matrix a[`i',5] = round(_se[gdpchange],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
* Pre/Post Keynesianism
* 1792-1944
local i = `i'+3
bootstrap, reps($r) seed(418):  reg  runagain gdpchange  if termed==. & year < 1945
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
bootstrap, reps($r) seed(418):  reg  win      gdpchange    if termed==.  & year < 1945
   matrix a[`i',4] = round(_b[gdpchange],.001)
   matrix a[`i',5] = round(_se[gdpchange],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
*1948-2016
  *no one retires
 bootstrap, reps($r) seed(418):  reg  win      gdpchange    if termed==. & year >= 1945
 local i = `i'+1
   matrix a[`i',4] = round(_b[gdpchange],.001)
   matrix a[`i',5] = round(_se[gdpchange],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
* Post Great Depression, FDR's election, measurement improvement
* 1796-1928
local i = `i'+2
bootstrap, reps($r) seed(418):  reg  runagain gdpchange  if termed==. & year < 1932
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
bootstrap, reps($r) seed(418):  reg  win      gdpchange    if termed==.  & year < 1932
   matrix a[`i',4] = round(_b[gdpchange],.001)
   matrix a[`i',5] = round(_se[gdpchange],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
* 1932-2016
  *no one retires
bootstrap, reps($r) seed(418):  reg  win      gdpchange    if termed==. & year >= 1932
 local i = `i'+1
   matrix a[`i',4] = round(_b[gdpchange],.001)
   matrix a[`i',5] = round(_se[gdpchange],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
 * Non-Early US History, cut at 1840
 local i = `i'+2
bootstrap, reps($r) seed(418): reg  runagain gdpchange  if termed==. & year <= 1840
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
 bootstrap, reps($r) seed(418): reg  win      gdpchange    if termed==.  & year <=1840
   matrix a[`i',4] = round(_b[gdpchange],.001)
   matrix a[`i',5] = round(_se[gdpchange],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
* 1840+-2016
 local i = `i'+1
 bootstrap, reps($r) seed(418): reg  runagain gdpchange  if termed==. & year >1840
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
 bootstrap, reps($r) seed(418): reg  win      gdpchange    if termed==.  & year >1840
   matrix a[`i',4] = round(_b[gdpchange],.001)
   matrix a[`i',5] = round(_se[gdpchange],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
* Pre/Post Civil War
* 1796-1856
  local i = `i'+2
bootstrap, reps($r) seed(418): reg  runagain gdpchange  if termed==. & year < 1860
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
 bootstrap, reps($r) seed(418): reg  win      gdpchange    if termed==.  & year <1860
   matrix a[`i',4] = round(_b[gdpchange],.001)
   matrix a[`i',5] = round(_se[gdpchange],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
* 1860-2016
 local i = `i'+1
 bootstrap, reps($r) seed(418): reg  runagain gdpchange  if termed==. & year >=1860
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
 bootstrap, reps($r) seed(418): reg  win      gdpchange    if termed==.  & year >=1860
   matrix a[`i',4] = round(_b[gdpchange],.001)
   matrix a[`i',5] = round(_se[gdpchange],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
* Year Control
 local i = `i'+3
bootstrap, reps($r) seed(418): reg  runagain gdpchange year  if termed==.
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
 bootstrap, reps($r) seed(418): reg  win gdpchange year if termed==.
   matrix a[`i',4] = round(_b[gdpchange],.001)
   matrix a[`i',5] = round(_se[gdpchange],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
* Year^2
 local i = `i'+1
bootstrap, reps($r) seed(418): reg  runagain gdpchange year year2 if termed==.
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
bootstrap, reps($r) seed(418):  reg  win gdpchange year year2  if termed==.
   matrix a[`i',4] = round(_b[gdpchange],.001)
   matrix a[`i',5] = round(_se[gdpchange],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
* Year^3
 local i = `i'+1
bootstrap, reps($r) seed(418): reg  runagain gdpchange  year year2 year3  if termed==. 
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
 bootstrap, reps($r) seed(418): reg  win gdpchange  year year2 year3  if termed==. 
   matrix a[`i',4] = round(_b[gdpchange],.001)
   matrix a[`i',5] = round(_se[gdpchange],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
* Removed Outliers
 local i = `i'+3
bootstrap, reps($r) seed(418): reg  runagain gdpchange  if outlier==.
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
 bootstrap, reps($r) seed(418): reg  win gdpchange  if outlier==.
   matrix a[`i',4] = round(_b[gdpchange],.001)
   matrix a[`i',5] = round(_se[gdpchange],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
* One-By-One drop and reestimate
 local i = `i'+1
  local smallest = 1000
  foreach j of num 1796(4)2016 {
   reg  runagain gdpchange  if termed==. & year != `j'
   if _b[gdpchange] < `smallest' {
     local smallest = _b[gdpchange]
	 local smallest_se = _se[gdpchange]
	 local smallest_r2 = e(r2)
	 local smallest_yearrun= `j'
     }
   }
   matrix a[`i',1] = round(`smallest',.001)
   matrix a[`i',2] = round(`smallest_se',.001)
   matrix a[`i',3] = round(`smallest_r2',.001)
  local smallest = 1000
  foreach j of num 1796(4)2016 {
   reg  win gdpchange  if termed==. & year != `j'
   if _b[gdpchange] < `smallest' {
     local smallest = _b[gdpchange]
	 local smallest_se = _se[gdpchange]
	 local smallest_r2 = e(r2)
	 local smallest_n = e(N)
	 local smallest_yearwin = `j'
     }
   }
   matrix a[`i',4] = round(`smallest',.001)
   matrix a[`i',5] = round(`smallest_se',.001)
   matrix a[`i',6] = round(`smallest_r2',.001)
   matrix a[`i',7] = round(`smallest_n',.001)
   display "smallest runagain coef. occurs when excluding " `smallest_yearrun' 
   display "smallest run-win coef. occurs when excluding "`smallest_yearwin'
* robust standard errors
  local i = `i'+3
 bootstrap, reps($r) seed(418):  reg  runagain gdpchange  if termed==. , robust
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
 bootstrap, reps($r) seed(418): reg  win gdpchange  if termed==. , robust
   matrix a[`i',4] = round(_b[gdpchange],.001)
   matrix a[`i',5] = round(_se[gdpchange],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
* plain vanilla standard errors
  local i = `i'+1
  reg  runagain gdpchange  if termed==.
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
 reg  win gdpchange  if termed==. , robust
   matrix a[`i',4] = round(_b[gdpchange],.001)
   matrix a[`i',5] = round(_se[gdpchange],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
*probit
   local i = `i'+1
 bootstrap, reps($r) seed(418): probit  runagain gdpchange  if termed==.
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
 bootstrap, reps($r) seed(418): probit  win gdpchange  if termed==.
   matrix a[`i',4] = round(_b[gdpchange],.001)
   matrix a[`i',5] = round(_se[gdpchange],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
* gdp growth indicator measure
 local i = `i'+3
bootstrap, reps($r) seed(418): reg  runagain positive_growth  if termed==.
   matrix a[`i',1] = round(_b[positive_growth],.001)
   matrix a[`i',2] = round(_se[positive_growth],.001)
   matrix a[`i',3] = round(e(r2),.001)
 bootstrap, reps($r) seed(418): reg  win positive_growth  if termed==.
   matrix a[`i',4] = round(_b[positive_growth],.001)
   matrix a[`i',5] = round(_se[positive_growth],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)

* Davis measure
 local i = `i'+1
bootstrap, reps($r) seed(418): reg  runagain davis  if termed==.
   matrix a[`i',1] = round(_b[davis],.001)
   matrix a[`i',2] = round(_se[davis],.001)
   matrix a[`i',3] = round(e(r2),.001)
bootstrap, reps($r) seed(418):  reg  win davis  if termed==.
   matrix a[`i',4] = round(_b[davis],.001)
   matrix a[`i',5] = round(_se[davis],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
* Updated Maddison Project
local i = `i'+1
bootstrap, reps($r) seed(418): reg  runagain updatemadproj  if termed==.
   matrix a[`i',1] = round(_b[updatemadproj],.001)
   matrix a[`i',2] = round(_se[updatemadproj],.001)
   matrix a[`i',3] = round(e(r2),.001)
bootstrap, reps($r) seed(418):  reg  win updatemadproj  if termed==.
   matrix a[`i',4] = round(_b[updatemadproj],.001)
   matrix a[`i',5] = round(_se[updatemadproj],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
* Ind_yr - Industrial production
 local i = `i'+1
bootstrap, reps($r) seed(418):  reg  runagain ind_yr  if termed==.
   matrix a[`i',1] = round(_b[ind_yr],.001)
   matrix a[`i',2] = round(_se[ind_yr],.001)
   matrix a[`i',3] = round(e(r2),.001)
bootstrap, reps($r) seed(418):  reg  win ind_yr  if termed==.
   matrix a[`i',4] = round(_b[ind_yr],.001)
   matrix a[`i',5] = round(_se[ind_yr],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001) 
* thorp_avgr 
 local i = `i'+1
bootstrap, reps($r) seed(418): reg  runagain thorp_avgr  if termed==. & year < 1926
   matrix a[`i',1] = round(_b[thorp_avgr],.001)
   matrix a[`i',2] = round(_se[thorp_avgr],.001)
   matrix a[`i',3] = round(e(r2),.001)
bootstrap, reps($r) seed(418):  reg  win thorp_avgr  if termed==. & year < 1926
   matrix a[`i',4] = round(_b[thorp_avgr],.001)
   matrix a[`i',5] = round(_se[thorp_avgr],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
* Term GDP 
local i = `i'+3
bootstrap, reps($r) seed(418): reg  runagain term_gdpchange  if termed==.
   matrix a[`i',1] = round(_b[term_gdpchange],.001)
   matrix a[`i',2] = round(_se[term_gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
bootstrap, reps($r) seed(418):  reg  win term_gdpchange  if termed==.
   matrix a[`i',4] = round(_b[term_gdpchange],.001)
   matrix a[`i',5] = round(_se[term_gdpchange],.001)
   matrix a[`i',6] = round(e(r2),.001)
   matrix a[`i',7] = round(e(N),.001)
* conservative run-again 
 local i = `i'+2
bootstrap, reps($r) seed(418): reg  min_runagain gdpchange  if termed==. 
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
mat2txt , matrix(a) saving(Output/Table_2) replace
   

*Table 3
matrix a = J(15,4,.) // Create matrix
matrix colnames a = b SE R2 N2
local i = 1
**Election year GDP Growth (Log Change)**
*DV: Incumbent Party Vote %
bootstrap, reps($r) seed(418): reg partyincshr gdpchange if year>1824
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
   matrix a[`i',4] = round(e(N),.001) 
   local i = `i'+1
bootstrap, reps($r) seed(418): reg partyincshr gdpchange if year>=1946
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
   matrix a[`i',4] = round(e(N),.001) 
   local i = `i'+1
bootstrap, reps($r) seed(418): reg partyincshr gdpchange if year<=1944
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
   matrix a[`i',4] = round(e(N),.001) 
   local i = `i'+2
*DV: Incumbent Candidate Vote %
replace incvote=incvote*100
bootstrap, reps($r) seed(418): reg incvote gdpchange if year>1824 & incballotgen==1
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
   matrix a[`i',4] = round(e(N),.001) 
   local i = `i'+1
bootstrap, reps($r) seed(418): reg incvote gdpchange if year>=1946 & incballotgen==1
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
   matrix a[`i',4] = round(e(N),.001) 
   local i = `i'+1
bootstrap, reps($r) seed(418): reg incvote gdpchange if year<=1944 & incballotgen==1
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
   matrix a[`i',4] = round(e(N),.001) 
   local i = `i'+2
**Year before election GDP Growth (Log Change)**
*DV: Incumbent Party Vote %
bootstrap, reps($r) seed(418): reg partyincshr gdpchangeyr3 if year>1824
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
   matrix a[`i',4] = round(e(N),.001) 
   local i = `i'+1
bootstrap, reps($r) seed(418): reg partyincshr gdpchangeyr3 if year>=1946
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
   matrix a[`i',4] = round(e(N),.001) 
   local i = `i'+1
bootstrap, reps($r) seed(418): reg partyincshr gdpchangeyr3 if year<=1944
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
   matrix a[`i',4] = round(e(N),.001) 
   local i = `i'+2
*DV: Incumbent Candidate Vote %
bootstrap, reps($r) seed(418): reg incvote gdpchangeyr3 if year>1824 & incballotgen==1
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
   matrix a[`i',4] = round(e(N),.001) 
   local i = `i'+1
bootstrap, reps($r) seed(418): reg incvote gdpchangeyr3 if year>=1946 & incballotgen==1
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
   matrix a[`i',4] = round(e(N),.001) 
   local i = `i'+1
bootstrap, reps($r) seed(418): reg incvote gdpchangeyr3 if year<=1944 & incballotgen==1
   matrix a[`i',1] = round(_b[gdpchange],.001)
   matrix a[`i',2] = round(_se[gdpchange],.001)
   matrix a[`i',3] = round(e(r2),.001)
   matrix a[`i',4] = round(e(N),.001) 
mat2txt , matrix(a) saving(Output/Table_3) replace
